﻿@using System.Web.UI.WebControls
@model string

@{
    ViewBag.Title = "title";
    Layout = null;
    string str = ViewBag.SwaggerJson;
}
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<style>
    .content {
        border: solid 1px gold;
    }
    table {
        border-collapse: collapse;
        border: solid 1px gray;
        
    }

    table td {
        border: solid 1px gray;
    }
</style>
<h2>title</h2>
<duv id="contentDiv">
</duv>
<script>
    String.prototype.format = function(args) {
        var result = this;
        if (arguments.length > 0) {
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    if (args[key] != undefined) {
                        var reg = new RegExp("({" + key + "})", "g");
                        result = result.replace(reg, args[key]);
                    }
                }
            }
            else {
                for (var i = 0; i < arguments.length; i++) {
                    if (arguments[i] != undefined) {
                        //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题，谢谢何以笙箫的指出
                        var reg = new RegExp("({)" + i + "(})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
        }
        return result;
    }
    String.format = function () {
        if (arguments.length == 0)
            return null;

        var str = arguments[0];
        for (var i = 1; i < arguments.length; i++) {
            var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
            str = str.replace(re, arguments[i]);
        }
        return str;
    }

    var models = ["ProjectPrep", "PriceMng", "CstMng", "TradeMng", "OpportunityMng"];
    var swaggerJson = eval(@Html.Raw(str));
    var paths = swaggerJson.paths;
    var html = "";
    var template = "<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>";
    $.each(models, function (i, n) {
        var table = "";
        var count = 0; 
        $.each(paths,function(key) {
            var item = paths[key];
            if (key.indexOf("/" + n + "/") != -1) {
                var post = item.post;
                table += template.format(post.tags[0], key, !post.summary ? "" : post.summary);
                count++;
            }
        });
        var guid = "@Guid.NewGuid()" + n;
        html = html + "<div class='content'>{0}   <span style='cursor: pointer;' showTable='1' tableGuid='{1}'>折叠/展开，数量：{3}</span>{2}</div>".format(n, guid, "<table style='display:none;' id=" + guid + ">" + table + "</table>", count);
    });
    $("#contentDiv").append(html);

    $("span[showTable='1']").click(function () {
        var $this = $(this);
        var tableGuid = $this.attr("tableGuid");
        $("#" + tableGuid).toggle();
    });
</script>
